package com.lipi.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lipi.common.utils.PageUtils;
import com.lipi.common.utils.Query;
import com.lipi.mall.product.dao.AttrAttrgroupRelationDao;
import com.lipi.mall.product.dao.AttrGroupDao;
import com.lipi.mall.product.entity.*;
import com.lipi.mall.product.service.*;
import com.lipi.mall.product.vo.AttrGroupWithAttrsVO;
import com.lipi.mall.product.vo.SkuItemVO;
import com.lipi.mall.product.vo.SpuItemAttrGroupVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationDao relationDao;

    @Autowired
    private AttrService attrService;

    @Autowired
    private SpuInfoService spuInfoService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private AttrGroupService attrGroupService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Autowired
    private ProductAttrValueService productAttrValueService;

    //不存在三级分类时，categoryId=0,查询所有数据
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();

        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }

        if (categoryId == 0) {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params)
                    , wrapper);

            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id", categoryId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrGroupWithAttrsVO> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //1、查出当前分类的所有属性分组
        List<AttrGroupEntity> attrGroupEntityList = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        //2、查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVO> collect = attrGroupEntityList.stream()
                .map(item -> {
                    AttrGroupWithAttrsVO vo = new AttrGroupWithAttrsVO();
                    BeanUtils.copyProperties(item, vo);

                    List<AttrEntity> attrEntityList = attrService.getRelationAttr(item.getAttrGroupId());
                    vo.setAttrs(attrEntityList);

                    return vo;
                })
                .collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<SpuItemAttrGroupVO> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {

        List<SpuItemAttrGroupVO> result = this.baseMapper.getAttrGroupWithAttrsBySpuId(spuId, catalogId);

        return result;
    }
}